﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace NetAcademia.Web.Mvc.ImageRendering
{


    public static class RawHelper
    {
        #region url
        public static HtmlString SqlImageRawUrl(this UrlHelper url, string key, object sqlParameters)
        {
            return SqlImageRawUrl(url, null, null, key, null, sqlParameters);
        }

        public static HtmlString SqlImageRawUrl(this UrlHelper url, string key, string downloadfilename, object sqlParameters)
        {
            return SqlImageRawUrl(url, null, null, key, downloadfilename, sqlParameters);
        }

        public static HtmlString SqlImageRawUrl(this UrlHelper url, string controller, string action, string key, string downloadfilename, object sqlParameters)
        {
            return new HtmlString(GetSqlDataUrl(url, controller, action, key, downloadfilename, sqlParameters));
        }
        #endregion


        #region <a>
        public static HtmlString SqlImageRawLink(this HtmlHelper html, string text, string key, object sqlParameters)
        {
            return SqlImageRawLink(html, null, null,text, key, null, sqlParameters, null);
        }

        public static HtmlString SqlImageRawLink(this HtmlHelper html, string text, string key, string downloadfilename, object sqlParameters)
        {
            return SqlImageRawLink(html, null, null, text, key, downloadfilename, sqlParameters, null);
        }

        public static HtmlString SqlImageRawLink(this HtmlHelper html, string text, string key, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawLink(html, null, null, text, key, null, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageRawLink(this HtmlHelper html, string text, string key, string downloadfilename, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawLink(html, null, null, text, key, downloadfilename, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageRawLink(this HtmlHelper html, string controller, string action, string text, string key, string downloadfilename, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawHtml(html, controller, action, text, key, downloadfilename, sqlParameters, htmlAttributes, RenderingTags.a);
        }
        #endregion


        #region img
        public static HtmlString SqlImageRaw(this HtmlHelper html, string key, object sqlParameters)
        {
            return SqlImageRaw(html, null, null, key, null, sqlParameters, null);
        }

        public static HtmlString SqlImageRaw(this HtmlHelper html, string key, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRaw(html, null, null, key, null, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageRaw(this HtmlHelper html, string controller, string action, string key, string downloadfilename, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawHtml(html, controller, action, null, key, downloadfilename, sqlParameters, htmlAttributes, RenderingTags.img);
        }
        #endregion


        #region input
        public static HtmlString SqlImageRawInput(this HtmlHelper html, string key, object sqlParameters)
        {
            return SqlImageRawInput(html, null, null, key, null, sqlParameters, null);
        }

        public static HtmlString SqlImageRawInput(this HtmlHelper html, string key, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawInput(html, null, null, key, null, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageRawInput(this HtmlHelper html, string controller, string action, string key, string downloadfilename, object sqlParameters, object htmlAttributes)
        {
            return SqlImageRawHtml(html, controller, action, null, key, downloadfilename, sqlParameters, htmlAttributes, RenderingTags.input);
        }
        #endregion


        private static string GetSqlDataUrl(this UrlHelper url, string controller, string action, string key, string downloadfilename, object sqlParameters)
        {
            if (string.IsNullOrWhiteSpace(action))
            {
                controller = "XAMLtoMVC";
                action = "SqlImageRaw";
            }


            string imageUrl = url.RouteUrl(new { controller = controller, action = action, id = key, downloadfilename = downloadfilename });

            if (sqlParameters != null)
            {
                var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(sqlParameters);
                StringBuilder sb = new StringBuilder(attributes.Count * 30);

                foreach (var item in attributes)
                {
                    sb.AppendFormat("&_{0}={1}", HttpUtility.UrlEncode(item.Key), HttpUtility.UrlEncode(string.Format("{0}", item.Value)));
                }

                if (!imageUrl.Contains('?')) //már van valami a querystring-ben
                {
                    sb.Remove(0, 1);
                    sb.Insert(0, '?');
                }
                imageUrl += sb.ToString();
            }
            return imageUrl;
        }

        private static HtmlString SqlImageRawHtml(HtmlHelper html, string controller, string action, string text, string key, string downloadfilename, object sqlParameters, object htmlAttributes, RenderingTags tag)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string imageUrl = GetSqlDataUrl(url, controller, action, key, downloadfilename, sqlParameters);

            return XamlToPngHelper.GenerateHtml(imageUrl, htmlAttributes, tag, text);

            
        }

    }
}
